import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib as mpl

np.random.seed(2)  # 设置随机种子
df = pd.DataFrame(np.random.rand(5, 4),
                  columns=['A', 'B', 'C', 'D'])  # 先生成0-1之间的5*4维度数据，再装入4列DataFrame中
# f=df.boxplot(sym='r*',patch_artist=True)
f = df.boxplot(patch_artist=True, return_type='dict')

# 这里共有四个box
color = ['k', 'g', 'r', 'deepskyblue']  # 有多少box就对应设置多少颜色
# color = mpl.colors.LinearSegmentedColormap.from_list('cmap', ['#9e9e9e', '#ef6c00'], 6)  # 有多少box就对应设置多少颜色

for box, c in zip(f['boxes'], color):
    # 箱体边框颜色
    box.set(color=c, linewidth=2)
    # 箱体内部填充颜色
    box.set(facecolor=c)

# 这里设置的是各个box的其他属性
for whisker in f['whiskers']:
    whisker.set(color='r', linewidth=2)
for cap in f['caps']:
    cap.set(color='g', linewidth=3)
for median in f['medians']:
    median.set(color='DarkBlue', linewidth=3)
for flier in f['fliers']:
    flier.set(marker='o', color='y', alpha=0.5)
plt.show()